{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'body'}}
<section class="container">
  <div class="row">
    <div class="col-md-5">
      <form id="new">
        <div class="form-group">
          <label for="exampleInputEmail1">选择板块</label>
          <select class="form-control" name="kind">
            <option>分享</option>
            <option>问答</option>
            <option>招聘</option>
            <option>客户端测试</option>
          </select>
        </div>
        <div class="form-group">
          <label for="exampleInputEmail1">标题</label>
          <input type="text" class="form-control" id="exampleInputEmail1" placeholder="标题" name="title">
        </div>
        <div class="form-group">
          <label for="exampleInputPassword1">内容</label>
          <textarea class="form-control" rows="3" name="content" placeholder="内容"></textarea>
        </div>
        <input type="hidden" name="author" value="{{ user._id }}">
        <input type="hidden" name="nickname" value="{{ user.nickname }}">
        <button type="submit" class="btn btn-warning">Submit</button>
      </form>
    </div>
  </div>
</section>
{{/block}}

{{ block 'script' }}
<script>
  $('#new').on('submit', function (e) {
    e.preventDefault()
    var formData = $(this).serialize()
    $.ajax({
      url: '/topics/new',
      type: 'post',
      data: formData,
      dataType: 'json',
      success: function (data) {
        var err_code = data.err_code
        if (err_code === 0) {
          alert('发表成功')
          window.location.href = '/'
        } else if (err_code === 500) {
          window.alert('服务器忙，请稍后重试！')
        }
      }
    })
  })
</script>
{{ /block }}
